package com.yimi.yinhepda.views.main.function_module

import android.os.Bundle
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import com.pda.ym.base.BaseActivity
import com.uce.pda.module.ConnectionResultLister
import com.yimi.yinhepda.R
import com.yimi.yinhepda.adapter.RecycleAdapter.AddFuncAdapter
import com.yimi.yinhepda.db.dao.FuncBtnTableDao
import com.yimi.yinhepda.db.table.FuncBtnTable
import com.yimi.yinhepda.utils.Common
import com.yimi.yinhepda.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_add_function.*
import kotlinx.android.synthetic.main.common_top_layout.*


/**
 * @author: lj
 * Date: 2020/3/31 14:18
 * Description:添加应用界面
 */
class AddFunctionActivity : BaseActivity() {

    override fun getPageName(): String {
        return "添加应用"
    }

    var adapter:AddFuncAdapter?=null

    var scanApplicationListSize:Int=0
    var insideApplicationList:Int=0
    var exceptionOperateApplicationListSize:Int=0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_function)

        scanApplicationListSize=intent.getIntExtra("scanApplicationListSize",0)
        insideApplicationList=intent.getIntExtra("insideApplicationList",0)
        exceptionOperateApplicationListSize=intent.getIntExtra("exceptionOperateApplicationListSize",0)

        tv_title.text = pageName

        val funcList =FuncTableUtils.getList(FuncBtnTableDao().findAll())
        //设置功能背景图片
        Common.setFuncPic(funcList)
         adapter = AddFuncAdapter(this@AddFunctionActivity, funcList,object:ConnectionResultLister<FuncBtnTable>{
            override fun getResult(errorCode: Int, data: FuncBtnTable?) {
                if (data!!.groupCode==Common.FUNC_GROUP_OPTER_CODE){ //交接扫描
                    if (scanApplicationListSize>=8){
                        ToastUtil.showToast("最多选择八个！")
                        return
                    }
                    else{
                        scanApplicationListSize++
                    }
                }
                else if(data!!.groupCode==Common.FUNC_GROUP_INSIDE_CODE){ //库内扫描
                    if (insideApplicationList>=8){
                        ToastUtil.showToast("最多选择八个！")
                        return
                    }
                    else{
                        insideApplicationList++
                    }
                }
                else if(data!!.groupCode==Common.FUNC_GROUP_EXCEPTION_CODE){ //质量管理
                    if (exceptionOperateApplicationListSize>=8){
                        ToastUtil.showToast("最多选择八个！")
                        return
                    }
                    else{
                        exceptionOperateApplicationListSize++
                    }
                }
                data?.isAdd = 1
                val result = FuncBtnTableDao().update(data)
                if(result != -1){
                    adapter!!.notifyDataSetChanged()
                }
            }
        })
        rv_add_func_list.layoutManager = LinearLayoutManager(this@AddFunctionActivity, LinearLayoutManager.VERTICAL, false)
        rv_add_func_list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        rv_add_func_list.adapter = adapter

        iv_back.setOnClickListener {
            finish()
        }
    }
}